package cn.com.zte.ngbc.dao.mybatis.dynamicsql;

import cn.com.zte.ngbc.base.expression.ExpressionEvaluator;
import cn.com.zte.ngbc.base.expression.ExpressionEvaluatorFactory;
import cn.com.zte.ngbc.base.util.CommUtil_;
import cn.com.zte.ngbc.base.util.StringUtil;
import cn.com.zte.ngbc.dao.mybatis.dynamicsql.node.AndSqlNodeHandler;
import cn.com.zte.ngbc.dao.mybatis.dynamicsql.node.IfSqlNodeHandler;
import cn.com.zte.ngbc.dao.mybatis.dynamicsql.node.OrSqlNodeHandler;
import cn.com.zte.ngbc.dao.mybatis.dynamicsql.node.SqlNodeHandler;
import cn.com.zte.ngbc.dao.mybatis.dynamicsql.node.StrSqlNodeHandler;
import cn.com.zte.ngbc.dao.mybatis.dynamicsql.node.WhereSqlNodeHandler;
import cn.com.zte.ngbc.frw.model.db.DbType;
import cn.com.zte.ngbc.frw.model.sql.dynamic.AndSqlNode;
import cn.com.zte.ngbc.frw.model.sql.dynamic.DynamicSqlSqlNode;
import cn.com.zte.ngbc.frw.model.sql.dynamic.IfSqlNode;
import cn.com.zte.ngbc.frw.model.sql.dynamic.OrSqlNode;
import cn.com.zte.ngbc.frw.model.sql.dynamic.SqlNode;
import cn.com.zte.ngbc.frw.model.sql.dynamic.StrSqlNode;
import cn.com.zte.ngbc.frw.model.sql.dynamic.WhereSqlNode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Element;

public class DynamicContext
{
  public  Element selectEle ;

public DynamicContext(Element selectEle ) {
	this.selectEle=selectEle;
}
  public DynamicSqlSqlNode getDynamicSql(List<DynamicSqlSqlNode> sqls)
  {
    if (StringUtil.isEmpty(sqls)) {
      throw new IllegalArgumentException("未配置sql语句");
    }
    DynamicSqlSqlNode defaultSqlNode = null;
    for (DynamicSqlSqlNode sql : sqls)
    {
      if (sql.getType() == null) {
        defaultSqlNode = sql;
      }
    }
    if ((defaultSqlNode == null) && (sqls.size() == 1)) {
      return (DynamicSqlSqlNode)sqls.get(0);
    }
    return defaultSqlNode;
  }
  
  public String getChildSql(List<SqlNode> logics, String logicName)
  {
    if (StringUtil.isEmpty(logics)) {
      return null;
    }
    StringBuilder sqlBuilder = new StringBuilder();
    boolean hasChildSql = false;
    boolean isLogic = false;
    if (StringUtil.isNotEmpty(logicName)) {
      isLogic = true;
    }
    for (SqlNode logic : logics)
    {	
      String childSql = getChildSql(logic);
      if (!StringUtil.isEmpty(childSql))
      {
        if ((isLogic) && (hasChildSql)) {
          sqlBuilder.append(logicName);
      	//this.selectEle.addCDATA("   "+logicName+"   ");
          
        }
        if ((isLogic) && ((childSql.indexOf("or") != -1) || (childSql.indexOf("and") != -1))) {
          sqlBuilder.append(" (" + childSql + ") ");
        } else {
          sqlBuilder.append(" " + childSql + " ");
        }
        hasChildSql = true;
      }
    }
    return sqlBuilder.toString().trim();
  }
  
  private String getChildSql(SqlNode logic)
  {
    if (logic == null) {
      return null;
    }
    SqlNodeHandler sqlNodeHandler;
    if ((logic instanceof StrSqlNode))
    {
      sqlNodeHandler = new StrSqlNodeHandler((StrSqlNode)logic);
    }
    else
    {

      if ((logic instanceof IfSqlNode))
      {
        sqlNodeHandler = new IfSqlNodeHandler((IfSqlNode)logic);
      }
      else
      {

        if ((logic instanceof AndSqlNode))
        {
          sqlNodeHandler = new AndSqlNodeHandler((AndSqlNode)logic);
        }
        else
        {
          if ((logic instanceof OrSqlNode))
          {
            sqlNodeHandler = new OrSqlNodeHandler((OrSqlNode)logic);
          }
          else
          {
            if ((logic instanceof WhereSqlNode)) {
              sqlNodeHandler = new WhereSqlNodeHandler((WhereSqlNode)logic);
            } else {
              throw new IllegalArgumentException("不支持该逻辑运算符[" + logic.getClass().getSimpleName() + "]");
            }
          }
        }
      }
    }
    return sqlNodeHandler.process(this);
  }
  
}